#-*- coding:utf-8 -*-

import requests
import urllib
import os
import ast

__verson__ = '1.0.5'

port = {
        'ser':10305,
        'con':10306,
        'dat':10307
}

class NetData:
    dat = port
    Instruction = {
        'data':'{ip}:{port}/data/{cmd}/'
    }

    ForData = {
        'token':None,
        'key':None,
        'value':None
    }

    def __req(self, cmd, key, data):
        rq = ForData
        rq['token'] = self.token
        rq['key'] = key
        rq['value'] = str(data)
        url = Instruction['data'].format(ip=self.ip, port=str(self.port), cmd=cmd)
        rs = requests.post('http://'+url, data = rq)
        rs = ast.literal_eval(rs.text)
        return rs
    
    def __init__(self, token, ip, port=port):
        self.ip = str(ip)
        self.port = int(port)
        self.token = str(token)
    
    def update(self, key, data):
        rs = self.__req('update', key, str(data))
        return rs

    def get(self, key):
        rs = self.__req('get', key, 'none')
        return rs

class LiHuaNet:
    port = port
    Instruction = {
        'connect':'127.0.0.1:{conport}/connect/{addr}/',
        'terminal':'127.0.0.1:{conport}/terminal/{req}/',
        'ser':'127.0.0.1:{conport}/net/{req}/',
        'dir':'127.0.0.1:{conport}/dir/{cmd}/{ip}/',
        'dir_file':'127.0.0.1:{conport}/dir/{cmd}/{ip}/{pat}/{pah}/',
    }

    def __req(self, url):
        try:
            rs = requests.get('http://'+url)
            return rs.text
        except:
            return '{"rezult":"APIError"}'
    
    def Terminal(self, cmd, **args):
        url = ''
        if cmd == 'connect':
            url = self.Instruction['connect'].format(conport=str(self.port['con']), addr=urllib.parse.quote(str(args['addr'])))
            if args.get('pwd'):
                url = url + args['pwd']
            rs = self.__req(url)
            return rs
        if cmd in ['con','net']:
            url = self.Instruction['terminal'].format(conport=str(self.port['con']), req=cmd)
        if cmd in ['terminal', 'info']:
            url = self.Instruction['ser'].format(conport=str(self.port['ser']), req=cmd)
        if cmd == 'name':
            url = self.Instruction['ser'].format(conport=str(self.port['ser']), req=cmd) + args['new']
        if url == '':return
        rs = ast.literal_eval(self.__req(url))
        return rs

    def File(self, cmd, ip, **args):
        for i in args:
            args[i] = args[i].replace('\\', ':-')
            args[i] = args[i].replace('/', ':-')
        url = ''
        if cmd == 'disk':
            url = self.Instruction['dir'].format(conport=str(self.port['con']), ip=ip, cmd=cmd)
        if cmd == 'list':
            url = self.Instruction['dir'].format(conport=str(self.port['con']), ip=ip, cmd=cmd) + args['path']
        if cmd == 'get':
            url = self.Instruction['dir_file'].format(conport=str(self.port['con']), ip=ip, cmd=cmd, pat=args['path'], pah=args['local'])
        if cmd == 'send':
            url = self.Instruction['dir_file'].format(conport=str(self.port['con']), ip=ip, cmd=cmd, pat=args['local'], pah=args['path'])
        if url == '':return
        rs = ast.literal_eval(self.__req(url))
        return rs

a = LiHuaNet()
addr = a.Terminal('info')
a.Terminal('connect', addr=addr)
print(a.Terminal('con'))
'''
code = a.Terminal('connect', addr={'name': 'DESKTOP-N75051N', 'ip': '192.168.1.12', 'port': 10306, 'verson': '1.9'})
print(code)
'''